home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.std.c
- Path: netcom.com!ahicks
- From: ahicks@netcom.com (Aaron Hicks at Netcom)
- Subject: SUMMARY: static and extern, and a variable be both ?
- Message-ID: <ahicksDKo2Ap.GKF@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- Date: Thu, 4 Jan 1996 17:08:48 GMT
- Sender: ahicks@netcom3.netcom.com
-
- Folks,
-
- I have summarized the answers I got about my problem with the static and
- extern keywords below. But first I would like to thank the following folks
- for being so kind as to enlighten me as to how this works.
-
- Crawford@cris.com
- seebs@intran.xerox.com
- fgae07@ods05.and.ifg.gmeds.com
- mjs@hubcap.clemson.edu
- weather@netside.com
-
- SUMMARY:
-
- If a variable is declared static inside a function this means to keep it
- in memory along with the globals and to allocate this space at run time. If
- a global is declared as static ( as I was doing ) it is hidden from all other
- modules ( .c files ). So a static in a function can be an extern for another
- module but a global static can not be seen from other modules.
-
- I have to admit that in hind sight a static global is kind of redundant in
- the context of what we wanted to do with this variable.
-
- After this message I have included the responces I got from the group. I
- have removed the header and most of the signatures of each message and any
- part that included my original post.
-
- Once again Thank You all for you help
-
- Aaron Hicks
- -------------------------------------------------------------------------
- Crawford@cris.com wrote
-
- When an object (variable, array) is declared with global scope
- (not declared within a function), it is permanently allocated and
- available for external linking. When you declare a global variable
- static, you are limiting access to it to the code in that
- translation unit (source file).
-
- I think you're confusing the meaning of static within
- functions, where it means "allocate at compile time", and the meaning
- of static for global variables: "make available only to this source
- file". All global variables are allocated at compile time, so the
- modifier of static is used to change the object's scope.
-
- It's confusing, but it makes sense after you've used it a
- while.
-
- -------------------------------------------------------------------------
- seebs@intran.xerox.com wrote
-
- You are confused.
- static *inside a function* causes only one copy of a variable to be
- created.
-
- static *outside* a function is specifically to prevent other modules
- (other .c files) from being allowed to see the variable.
-
- -s
-
- ------------------------------------------------------------------------
- fgae07@ods05.and.ifg.gmeds.com wrote
-
- When you declare something static to a module, that means only the
- functions in that module can reference the variable. Only functions in
- the raster.c module can refer to bit_fail_array_table and have it
- defined. When you declare bit_fail_array_table to be extern in
- raster_analysis.c, it expects another module to actually define it,
- which causes storage to ba allocated. Since the variable in raster.c is
- only visible within raster.c, raster_analysis.c cannot see it, hence the
- message about the variable being undefined. When you took off the
- static declaration in raster.c, this made the variable a system-wide
- global, visible to anybody who has an external declaration for it.
-
- Hope this helps.
-
- --
- Joe Gilliatt
- fgae07@ods05.and.ifg.gmeds.com
-
- ------------------------------------------------------------------------
- mjs@hubcap.clemson.edu wrote
-
- If bit_fail_array_table is at file scope (outside of any function)
- then it has static extent (i.e. it lives for the entire program), no
- matter what. In this context, the static modifier means that the
- variable name is invisible outside of the file. This is a problem if
- the variable is declared extern in another file. The solution is
- simply to not declare it static in the defining file. But it's good
- practice to explicitly initialize it, even if it is to be initialized
- to zero, just to indicate that it is the defining instance.
-
- If a variable has block scope (inside a function), it can never be
- visible outside the block it appears in, and then the static modifier
- indicates that it has static (as opposed to automatic) extent.
-
- Matthew Saltzman
- Clemson University Math Sciences
- mjs@clemson.edu
-
- -----------------------------------------------------------------------------
- weather@netside.com wrote
-
- The purpose of using the 'static' keyword on a global variable is *precisely*
- to prevent it from being accessed outside of the translation unit, i.e. other
- source files. Using static within a function has another purpose in that
- the variable so declared will retain its value. You must remember that
- local variables within functions are generally given temporary storage on the
- stack, which is lost when the function returns. Declaring a local variable
- as static will cause it to be stored in memory alongside global variables
- even though it still retains local scope. Using 'static' on global variables
- only makes its scope or 'viewability' limited. In short, get rid of the
- 'static'. Hope this helps.
-
- R.Weathersby
-
- P.S. I hope I interpreted your message correctly.
-
-
-
-